import axios from 'axios';
import hotspot1 from '../assets/hotspot.jpg';
import hotspot2 from '../assets/hotspot2.jpg';
import base from './config';
// 设置基础URL，实际开发时替换为真实的API地址
// const baseURL = 'http://localhost:8080';
const baseURL= `${base}`

// 获取热门帖子
//done
export const fetchHotPosts = async () => {
  try {
    const requestData = {
      timeRange: "month",
      sortBy: "viewCount",
      isHot: true,
      page: 1,
      pageSize: 8
    };
    const response = await axios.post(`${baseURL}/api/post/list`, requestData, {
        headers: {
          'Content-Type': 'application/json'
        }
      });
     return response.data;
    } catch (error) {
    console.error('获取热门帖子失败:', error);
    throw error;
  }
};

// 获取轮播图数据
//test
export const fetchCarouselItems = async () => {
  try {
    // 模拟API调用
    // const response = await axios.get(`${baseURL}/carousel`);
    // return response.data;
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve([
          {
            id: 1,
            image: hotspot1,
            title: '沙丘2震撼上映',
            link: '/movie/dune2'
          },
          {
            id: 2,
            image: hotspot2,
            title: '奥本海默获奥斯卡最佳影片',
            link: '/movie/oppenheimer'
          },
        ]);
      }, 800);
    });
  } catch (error) {
    console.error('获取轮播图数据失败:', error);
    throw error;
  }
};

export const fetchRecommendedTorrents = async () => {
    try {
        const response = await axios.get(`${baseURL}/torrent/recommend`);
        return response.data;
    } catch (error) {
        console.error('Error fetching recommended torrents:', error);
        throw error;
    }
};